<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-button-group>
          <el-button type="primary" icon="el-icon-back" @click="backward">返回</el-button>
          <el-button type="primary" icon="el-icon-share" @click="addDialogVisible = true">配置产能</el-button>
        </el-button-group>
      </el-row>
      <el-row :gutter="25">
      </el-row>
      <!-- 产品列表 -->
      <el-table :data="configList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="产品ID" prop="ep_pro_id"></el-table-column>
        <el-table-column label="产品名称" prop="ep_pro_name"></el-table-column>
        <el-table-column label="产品编号" prop="ep_pro_code"></el-table-column>
        <el-table-column label="产品产能" prop="ep_capa"></el-table-column>
      </el-table>

      <!-- 创建新产品对话框 -->
      <el-dialog title="添加设备" :visible.sync="addDialogVisible" width="50%"
                 @close="addDialogClosed">
        <!-- 内容主体区域 -->
        <el-form :model="configForm" :rules="addFormRules"
                 ref="configFormRef" label-width="70px">
          <!-- 产品名称 -->
          <el-form-item label="产品名称" prop="ep_pro_id">
            <el-select v-model="configForm.ep_pro_id">
              <el-option :value="product.pro_id" v-for="product in productList">{{product.pro_name}}</el-option>
            </el-select>
          </el-form-item>

          <!-- 产品产能 -->
          <el-form-item label="产品产能" prop="ep_capa">
            <el-input v-model="configForm.ep_capa"></el-input>
          </el-form-item>

        </el-form>
        <!-- 内容底部区域 -->
        <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addConfig">确 定</el-button>
      </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      fac_id:'',
      equ_id:'',
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
        fac:'',
        equ:'',
      },
      addFormRules:{},
      configList: [],// 用户列表
      configForm:{
        ep_pro_id:'',
        ep_equ_id:'',
        ep_fac_id:'',
        ep_capa:'',
      },//新增配置表单
      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
      // 控制修改用户对话框显示/隐藏
      editDialogVisible:false,
      // 修改用户信息
      productList:[],
    }
  },
  created() {
    this.getTempEquId();
    this.getTempFacId();
    this.getConfigList();
    this.getProductList();
  },
  methods: {
    getTempFacId(){
      this.fac_id = window.sessionStorage.getItem("fac_id");
    },
    getTempEquId(){
      this.equ_id = window.sessionStorage.getItem("equ_id");
    },
    async getConfigList() {
      // 调用post请求
      this.queryInfo.fac = this.fac_id;
      console.log(this.queryInfo.fac)
      this.queryInfo.equ = this.equ_id;
      console.log(this.queryInfo.equ)
      const { data: res } = await this.$http.get("myEquip/getConfigList", {
        params: this.queryInfo
      });
      this.configList = res.data; // 将返回数据赋值
      this.total = res.numbers; // 总个数
    },
    //获取当前fac_id

    //获取当前ord_id

    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getConfigList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getConfigList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.configFormRef.resetFields();// 重置表单项
    },
    // 添加产品类别
    addConfig(){
      this.getProductList();
      this.$refs.configFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        //绑定当前配置信息
        this.configForm.ep_equ_id = this.equ_id;
        this.configForm.ep_fac_id = this.fac_id;
        const {data:res} = await this.$http.post("myEquip/addConfig",this.configForm);
        if (res !== "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getConfigList();
      })
    },
    async getProductList(){
      const { data: res } = await this.$http.post("product/allProductList");
      this.productList = res.data;
      console.log(this.productList);
    },
    // 展示修改框
    async showEditDialog(id){

      const {data:res} = await this.$http.post("product/getUpdate?id="+id);
      // if (res != "success") {
      // userinfo.state = !userinfo.state;
      // return this.$message.error("操作失败！！！");
      // }
      // this.$message.success("操作成功！！！");

      this.editForm = res;
      this.editDialogVisible = true;
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editProductInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("product/editProduct",this.editForm);
        console.log(res);
        if (res !== "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getConfigList();
      });
    },
    // 删除按钮
    async deleteProduct(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.post("product/deleteProduct?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getConfigList();
    },
    //返回上一页
    backward(){
      this.$router.push("/myEquip");
    },
  },
};
</script>
<style lang="less" scoped>
</style>